package dbUtil;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ColumnUtil {
    public static Object columnType(ResultSet rs, String columName, Field f) throws SQLException {
        Object o = null;
        if (f.getType().equals(Float.class) || f.getType().equals(Float.TYPE)) {
            o = rs.getFloat(columName);
        } else if (f.getType().equals(Double.class) || f.getType().equals(Double.TYPE)) {
            o = rs.getDouble(columName);
        } else if (f.getType().equals(Integer.class) || f.getType().equals(Integer.TYPE)) {
            o = rs.getInt(columName);
        } else if (f.getType().equals(Boolean.class) || f.getType().equals(Boolean.TYPE)) {
            o = rs.getBoolean(columName);
        } else if (f.getType().equals(Byte.class) || f.getType().equals(Byte.TYPE)) {
            o = rs.getByte(columName);
        } else if (f.getType().equals(Short.class) || f.getType().equals(Short.TYPE)) {
            o = rs.getShort(columName);
        } else if (f.getType().equals(Long.class) || f.getType().equals(Long.TYPE)) {
            o = rs.getLong(columName);
        } else if (f.getType().equals(char.class)) {
            o = rs.getCharacterStream(columName);
        } else if (f.getType().equals(String.class)) {
            o = rs.getString(columName);
        } else if (f.getType().equals(int.class)) {
            o = rs.getInt(columName);
        } else {
            o = rs.getObject(columName);
            o = getTime(o, f);
        }
        return o;
    }
    private static Object getTime(Object o, Field f) throws SQLException {
        if (o instanceof java.util.Date) {
            String type = f.getType().getName();
            if ("java.sql.Date".equals(type)) {
                o = new Date(((java.util.Date) o).getTime());
            } else if ("java.sql.Time".equals(type)) {
                o = new java.sql.Time(((java.util.Date) o).getTime());
            }
        }
        return o;
    }
}
